package xmlItunes;

import java.util.HashMap;
import java.util.Random;

import javax.xml.stream.XMLStreamReader;

public class Track 
{
	/*
	private int 	Track_ID =0;
	private String 	Name ="";
	private String 	Artist ="";
	private String 	Album_Artist ="";
	private String 	Album="";
	private String 	Grouping="";
	private String 	Genre="";
	private String 	Kind="";
	private int 	Size =0;
	private int 	Total_Time=0;
	private int 	Disc_Number=0;
	private int 	Disc_Count=0;
	private int 	Track_Number=0;
	private int 	Track_Count=0;
	private int 	Year=0;
	private String 	Date_Modified="";
	private String 	Date_Added="";
	private int 	Bit_Rate=0;
	private int 	Sample_Rate=0;
	private int 	Play_Count=0;
	private BigInteger 	Play_Date;
	private String 	Play_Date_UTC="";
	private String 	Release_Date="";
	private int 	Rating=0;
	private int 	Normalization=0;
	private int 	Artwork_Count=0;
	private String 	Persistent_ID="";
	private String 	Track_Type="";
	private boolean Protected =false;
	private int 	File_Creator=0;
	private String 	Location =""; 
	private int 	File_Folder_Count=0;
	private int 	Library_Folder_Count =0;
	*/
	
	private HashMap<String ,String> attributes =  new HashMap<String ,String>();
	
	public Track( XMLStreamReader reader )
	{
		try
		{
			int flag = 0;
			final int FLAG_NONE = 0;
			final int FLAG_KEY = 1;String key="";
			final int FLAG_VALUE=2;String value="";
	
			boolean state = true;
			while (reader.hasNext() && state) 
			{
				int type = reader.next();
	
				switch (type) 
				{
				// Si c'est un d�but d'el�ment, on garde son type
				case XMLStreamReader.START_ELEMENT:
					if( reader.getLocalName().equals("key") )
					{
						flag = FLAG_KEY;
					}
					else if (
							   reader.getLocalName().equals("data")
							|| reader.getLocalName().equals("date")
							|| reader.getLocalName().equals("real")
							|| reader.getLocalName().equals("integer")
							|| reader.getLocalName().equals("string")
							|| reader.getLocalName().equals("true")
							|| reader.getLocalName().equals("false")
							)
					{
						flag = FLAG_VALUE;
					}
					else
					{
						flag = FLAG_NONE;
					}
					break;
	
				// Si c'est du texte ou une zone CDATA ...
				case XMLStreamReader.CDATA:
				case XMLStreamReader.CHARACTERS:
					switch (flag) 
					{						
					case FLAG_NONE:
						// et que ce n'est pas une cha�ne de blancs
						if (!(reader.isWhiteSpace()))
						{
						//	System.out.println("track : contenu sans flag ! =>"+reader.getText());
						}
						break;
						
					case FLAG_KEY:
						// et que ce n'est pas une cha�ne de blancs
						if (!(reader.isWhiteSpace()))
							key = reader.getText() ;
						break;						

					case FLAG_VALUE:
						// et que ce n'est pas une cha�ne de blancs
						if (!(reader.isWhiteSpace()))
						{
							value = reader.getText() ;
							this.setAttribut(key, value);
						}
						break;						
						
					default:
						if (!(reader.isWhiteSpace()))
							System.out.println(reader.getText());
					}
					break;
				case XMLStreamReader.END_ELEMENT:
					// Si c'est la fin de la balise user, on change l'indicateur de boucle
					if (reader.getLocalName().equals("dict"))
					{
						state = false;
					}
					break;
				}
			}
		}catch(Exception e ){ e.printStackTrace(); }
		
		attributes.put("Rating" , new Random().nextInt(20)+"");
	}

	
	public void setAttribut( String key, String value )
	{
		
		this.attributes.put(key, value);
		
		/*
				if( key.equals("Album") ) this.Album = value;
		else 	if( key.equals("Artist") ) this.Album_Artist = value;
		else 	if( key.equals("Album Artist") ) this.Artist = value;
		else 	if( key.equals("Artwork Count") ) this.Artwork_Count = Integer.parseInt(value);
		else 	if( key.equals("Bit Rate") ) this.Bit_Rate= Integer.parseInt(value);
		else 	if( key.equals("Date Added") ) this.Date_Added = value;
		else 	if( key.equals("Date Modified") ) this.Date_Modified = value;
		else 	if( key.equals("Disc Count") ) this.Disc_Count = Integer.parseInt(value);
		else 	if( key.equals("Disc Number") ) this.Disc_Number =Integer.parseInt(value);
		else 	if( key.equals("File Creator") ) this.File_Creator = Integer.parseInt(value);
		else 	if( key.equals("File Folder Count") ) this.File_Folder_Count = Integer.parseInt(value);
		else 	if( key.equals("Genre") ) this.Genre = value;
		else 	if( key.equals("Grouping") ) this.Grouping = value;
		else 	if( key.equals("Kind") ) this.Kind = value;
		else 	if( key.equals("Library Folder Count") ) this.Library_Folder_Count = Integer.parseInt(value);
		else 	if( key.equals("Location") ) this.Location =  value ;
		else 	if( key.equals("Name") ) this.Name =  value ;
		else 	if( key.equals("Normalization") ) this.Normalization =  Integer.parseInt( value );
		else 	if( key.equals("Persistent ID") ) this.Persistent_ID =  ( value );
		else 	if( key.equals("Play Count") ) this.Play_Count =  Integer.parseInt( value );
		else 	if( key.equals("Play Date") ) this.Play_Date =  new BigInteger( value );
		else 	if( key.equals("Play Date UTC") ) this.Play_Date_UTC = value ;
		else 	if( key.equals("Protected") ) this.Protected = Boolean.parseBoolean(value) ;
		else 	if( key.equals("Track ID") ) this.Track_ID = Integer.parseInt(value);
		*/	
	}

	
	public String getAttribute(String lequel)
	{
		return this.attributes.get(lequel);	
	}
	
	
	public String toString()
	{
		String s ="" ;
		
		for( String clef : this.attributes.keySet() )
		{
			s+="\n"+clef+" : "+ this.attributes.get(clef);
		}
		
		return s;
		
		/*
		String s ="";
		s +="\nid : "+ this.Track_ID ;
		s +="\nAlbum : "+ this.Album ;
		s +="\nArtist : "+ this.Album_Artist;
		s +="\nAlbum Artist : "+ this.Artist ;
		s +="\nArtwork Count : "+ this.Artwork_Count ;
		s +="\nBit Rate : "+ this.Bit_Rate;
		s +="\nDate Added : "+ this.Date_Added ;
		s +="\nDate Modified : "+ this.Date_Modified ;
		s +="\nDisc Count : "+ this.Disc_Count ;
		s +="\nDisc Number : "+ this.Disc_Number ;
		s +="\nFile Creator : "+ this.File_Creator ;
		s +="\nFile Folder Count : "+ this.File_Folder_Count ;
		s +="\nGenre : "+ this.Genre ;
		s +="\nGrouping : "+ this.Grouping ;
		s +="\nKind : "+ this.Kind;
		s +="\nLibrary Folder Count : "+ this.Library_Folder_Count ;
		s +="\nLocation : "+this.Location ;
		s +="\nName : "+ this.Name ;
		s +="\nNormalization : "+ this.Normalization ;
		s +="\nPersistent ID : "+ this.Persistent_ID ;
		s +="\nPlay Count : "+ this.Play_Count ;
		s +="\nPlay Date : "+ this.Play_Date ;
		s +="\nPlay Date UTC : "+ this.Play_Date_UTC ;
		s +="\nProtected : "+this.Protected ;

		return s;
		*/
	}

}
